<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <div style="margin-top: 15px">
        <el-input
          placeholder="请输入内容"
          class="input-with-select"
          v-model="queryF.query"
          @clear="getGoodsList"
          clearable
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="getGoodsList"
          ></el-button>
          <!-- 添加点击重新调用数据 -->
        </el-input>
      </div>

      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column
          prop="goods_id"
          label="#"
          width="50"
        ></el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
          width="500"
        ></el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格(元)"
        ></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column prop="" label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.goods_id)"
              icon="el-icon-delete"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryF.pagenum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="queryF.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryF: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 总条数
      total: 0,
      // 列表数据
      tableData: []
    }
  },
  created() {
    // 首次页面加载渲染
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      //获取商品列表
      const response = await this.$http.get('goods', { params: this.queryF })
      console.log(response)
      const res = response.data.data
      ;(this.tableData = res.goods), (this.total = res.total)
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.queryF.pagesize = val
      this.getGoodsList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.queryF.pagenum = val
      this.getGoodsList()
    },

    handleDelete(id) {
      console.log(id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          //调用删除接口 删除数据
          // 删除成功后提示删除成功
          // const data = await this.$http.delete(`goods/${id}`)
          const {
            data: { data, meta }
          } = await this.$http.delete(`goods/${id}`)
          // const data = {data:'123',msg:'234'}
          // const {data:res} = data
          console.log(meta)
          if (meta.status === 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getGoodsList()
          } else {
            this.$message({
              type: 'error',
              message: '删除失败!'
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style lang="less" scoped>
.input-with-select {
  width: 500px;
}
</style>
